package com.paipaideli.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.paipaideli.MyApplication;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.base.IEventBus;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.common.utils.preferrnces.FastData;
import com.paipaideli.ui.home.HomeFragment;
import com.paipaideli.ui.home.LotsFragment;
import com.paipaideli.ui.home.MineFragment;
import com.paipaideli.ui.home.ProfitFragment;
import com.paipaideli.ui.login.LoginActivity;
import com.paipaideli.ui.mine.safety.event.LogoutEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindViews;
import butterknife.OnClick;

public class MainActivity extends BasePresenterActivity implements IEventBus {

    private HomeFragment homeFragment;
    private LotsFragment lotsFragment;
    private ProfitFragment profitFragment;
    private MineFragment mineFragment;

    @BindViews({R.id.main_home, R.id.main_lots, R.id.main_profit, R.id.main_mine})
    List<LinearLayout> linearLayout;

    //声明一个long类型变量：用于存放上一点击“返回键”的时刻
    private long mExitTime;

    public static void openSingleTop(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        selected(R.id.main_home);
    }

    @OnClick({R.id.main_home, R.id.main_lots, R.id.main_profit, R.id.main_mine})
    public void onItemClick(View view) {
        switch (view.getId()) {
            case R.id.main_home:
                MyApplication.isHome = 0;
                selected(R.id.main_home);
                break;
            case R.id.main_lots:
                MyApplication.isHome = 1;
                selected(R.id.main_lots);
                break;
            case R.id.main_profit:
                MyApplication.isHome = 2;
                if (FastData.isLogin()) {
                    selected(R.id.main_profit);
                } else {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.main_mine:
                MyApplication.isHome = 3;
                if (FastData.isLogin()) {
                    selected(R.id.main_mine);
                } else {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onEvent(LogoutEvent logoutEvent) {
        switch (logoutEvent.getCheck()) {
            case 0:
                selected(R.id.main_home);
                break;
            case 1:
                selected(R.id.main_lots);
                break;
            case 2:
                if (FastData.isLogin()) {
                    selected(R.id.main_profit);
                } else {
                    LoginActivity.open(getCurrentActivity());
                }
                break;
            case 3:
                if (FastData.isLogin()) {
                    selected(R.id.main_mine);
                } else {
                    LoginActivity.open(getCurrentActivity());
                }
                break;
        }

    }

    public void selected(int id) {
        for (LinearLayout views : linearLayout) {
            if (views.getId() == id) {
                views.setSelected(true);
                initFragment(id);
            } else {
                views.setSelected(false);
            }
        }
    }

    //显示fragment
    public void initFragment(int id) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch (id) {
            case R.id.main_home:
                if (homeFragment == null) {
                    homeFragment = HomeFragment.newInstance();
                    transaction.add(R.id.fragment_container, homeFragment);
                }
                transaction.show(homeFragment);
                break;
            case R.id.main_lots:
                if (lotsFragment == null) {
                    lotsFragment = LotsFragment.newInstance();
                    transaction.add(R.id.fragment_container, lotsFragment);
                }
                transaction.show(lotsFragment);
                break;
            case R.id.main_profit:
                if (profitFragment == null) {
                    profitFragment = ProfitFragment.newInstance();
                    transaction.add(R.id.fragment_container, profitFragment);
                }
                transaction.show(profitFragment);
                break;
            case R.id.main_mine:
                if (mineFragment == null) {
                    mineFragment = MineFragment.newInstance();
                    transaction.add(R.id.fragment_container, mineFragment);
                }
                transaction.show(mineFragment);
                break;
            default:
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    //切换
    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (lotsFragment != null) {
            transaction.hide(lotsFragment);
        }
        if (profitFragment != null) {
            transaction.hide(profitFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if (homeFragment == null && fragment instanceof HomeFragment)
            homeFragment = (HomeFragment) fragment;
        if (lotsFragment == null && fragment instanceof LotsFragment)
            lotsFragment = (LotsFragment) fragment;
        if (mineFragment == null && fragment instanceof MineFragment)
            mineFragment = (MineFragment) fragment;
        if (profitFragment == null && fragment instanceof ProfitFragment)
            profitFragment = (ProfitFragment) fragment;
    }


    /**
     * 双击返回键退出
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                ToastUtil.show("再按一次退出程序");
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
